package com.drcoffee.drcoffeemall.service.impl;

import com.drcoffee.drcoffeemall.entry.Machine;
import com.drcoffee.drcoffeemall.entry.Result;
import com.drcoffee.drcoffeemall.entry.Type;
import com.drcoffee.drcoffeemall.mapper.FilePathMapper;
import com.drcoffee.drcoffeemall.mapper.MachineMapper;
import com.drcoffee.drcoffeemall.mapper.TypeMapper;
import com.drcoffee.drcoffeemall.service.MachineService;
import com.drcoffee.drcoffeemall.util.FileUploadDownloadUtils;
import com.drcoffee.drcoffeemall.util.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;


/*
 * 机器
 * */
@Service
@Transactional
//@DS("mysql") // yml中配置的数据源名
public class MachineServiceImpl implements MachineService {

    final
    MachineMapper machineMapper;

    @Value("${nginx_file}")
    private String init_path;

    final
    TypeMapper typeMapper;

    @Autowired
    FilePathMapper filePathMapper;

    final
    OSSUtil ossUtil;

    public MachineServiceImpl(MachineMapper machineMapper, OSSUtil ossUtil, TypeMapper typeMapper) {
        this.machineMapper = machineMapper;
        this.ossUtil = ossUtil;
        this.typeMapper = typeMapper;
    }


    /*
     * 查询机器
     * */
    @Override
    public Map<String, Object> getMachine(Integer type_id, String search) {
        List<Machine> machineList = new ArrayList<>();
        if (type_id == 0) {
            // All
            machineList = machineMapper.getMachineAll("%" + search + "%");
//            machineList = machineMapper.getMachineAll(machine);
        } else {
            // 根据系列查询
            machineList = machineMapper.getMachine(type_id, "%" + search + "%");
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("result", "1");
        resultMap.put("machines", machineList);
        return resultMap;
    }

    /*
     * 根据id查询机器
     * */
    @Override
    public Map<String, Object> getMachineById(Integer id) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            Machine machine = machineMapper.getMachineById(id);
            resultMap.put("result", true);
            resultMap.put("machine", machine);
            resultMap.put("message", "Success");
        } catch (Exception e) {
            resultMap.put("result", false);
            resultMap.put("error", e.toString());
            e.printStackTrace();
        }
        return resultMap;
    }


    @Override
    public Map<String, Object> getMachineAll() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<Machine> machines = machineMapper.getMachineAll("%%");
            resultMap.put("result", true);
            resultMap.put("machines", machines);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
            resultMap.put("error", e.toString());
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> deleteOld(Integer id) {
        Map<String, Object> resultMap = new HashMap<>();
        Machine machine = machineMapper.getMachineById(id);
        try {
            if (!Objects.equals(machine.getImage_url(), "")) {
                boolean delResult = FileUploadDownloadUtils.deleteFile(machine.getImage_url());
                if (delResult) {
                    resultMap.put("result", true);
                } else {
                    resultMap.put("result", false);
                }
            } else {
                resultMap.put("result", true);
            }
        } catch (Exception e) {
            resultMap.put("error", e.toString());
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> updateMachine(Machine machine) {
        Map<String, Object> resultMap = new HashMap<>();
        machine.setImage_url(init_path + machine.getImage_url().replaceAll("\\\\", "/"));
        boolean result = machineMapper.updateMachine(machine);
        resultMap.put("result", result);
        if (result) {
            resultMap.put("message", "修改成功");
        } else {
            resultMap.put("message", "修改失败");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> updateMachineText(Machine machine) {
        Map<String, Object> resultMap = new HashMap<>();
        boolean result = machineMapper.updateMachineText(machine);
        resultMap.put("result", result);
        if (result) {
            resultMap.put("message", "修改成功");
        } else {
            resultMap.put("message", "修改失败");
        }
        return resultMap;
    }

    @Override
    public ResponseEntity<Object> insertData(MultipartFile file, String fileName, Map<String, Object> machine) {
        Type type = typeMapper.getTypeById((Integer) machine.get("type_id"));
        String uploadPath = filePathMapper.getUploadPath(type.getPath_bsh(), 1);
        Result upload = ossUtil.upload(file, uploadPath, fileName);
        if (upload.getResult() != 1) {
            return new ResponseEntity<>(false, HttpStatus.OK);
        }
        machine.put("image_url", upload.getObject());
        machineMapper.insertData(machine);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

}
